package com.leoman.work.service.impl;

import com.leoman.adsindex.dao.AdsIndexDao;
import com.leoman.adsindex.entity.AdsIndex;
import com.leoman.banner.dao.BannerDao;
import com.leoman.banner.entity.Banner;
import com.leoman.comment.service.WorkCommentService;
import com.leoman.common.service.impl.GenericManagerImpl;
import com.leoman.entity.Constant;
import com.leoman.floatwin.dao.FloatWinDao;
import com.leoman.floatwin.entity.FloatWin;
import com.leoman.work.dao.WorkNovelDao;
import com.leoman.work.dao.WorkVideoDao;
import com.leoman.work.entity.WorkComment;
import com.leoman.work.entity.WorkNovel;
import com.leoman.work.entity.WorkVideo;
import com.leoman.work.service.WorkNovelService;
import com.leoman.work.service.WorkVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 蔡琦 on 2016/6/21 0021.
 *
 */
@Service
public class WorkNovelServiceImpl extends GenericManagerImpl<WorkNovel, WorkNovelDao> implements WorkNovelService {

    @Autowired
    private WorkNovelDao workNovelDao;

    @Autowired
    private WorkCommentService workCommentService;

    @Autowired
    private BannerDao bannerDao;

    @Autowired
    private AdsIndexDao adsIndexDao;

    @Autowired
    private FloatWinDao floatWinDao;

    @Override
    public List<WorkNovel> findByWorkId(Long workId) {
        return workNovelDao.findByWorkId(workId);
    }

    @Override
    public Page<WorkNovel> page(final Long workId, Integer pageNum, Integer pageSize) {
        PageRequest pageRequest = new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id");

        Page<WorkNovel> page = workNovelDao.findAll(new Specification<WorkNovel>() {
            @Override
            public Predicate toPredicate(Root<WorkNovel> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate result = null;
                List<Predicate> predicateList = new ArrayList<Predicate>();

                if (workId != null) {
                    Predicate pre = cb.equal(root.get("work").get("id").as(Long.class), workId);
                    predicateList.add(pre);
                }

                Predicate pre = cb.equal(root.get("status").as(Integer.class), 0);
                predicateList.add(pre);

                if (predicateList.size() > 0) {
                    result = cb.and(predicateList.toArray(new Predicate[]{}));
                }

                if (result != null) {
                    query.where(result);
                }
                return query.getGroupRestriction();
            }

        }, pageRequest);

        return page;
    }

    @Override
    @Transactional
    public Integer deleteNovel(Long novelId) {
        WorkNovel workNovel = workNovelDao.findOne(novelId);

        // 是否链接到广告banner
        List<Banner> bannerList = bannerDao.findByTypeAndWorkId(Constant.POSITION_002, novelId);
        if (bannerList.size() != 0) {
            return 2;
        }

        // 是否链接到首页推荐
        List<AdsIndex> adsIndexList = adsIndexDao.findByTypeAndWorkId(Constant.POSITION_002, novelId);
        if (adsIndexList.size() != 0) {
            return 2;
        }

        // 是否链接到浮窗广告
        List<FloatWin> floatWinList = floatWinDao.findByTypeAndWorkId(Constant.POSITION_002, novelId);
        if (floatWinList.size() != 0) {
            return 2;
        }

        // 查询小说章节评论
        List<WorkComment> workCommentList = workCommentService.findList(workNovel.getId(), Constant.CATEGORY_TYPE_005, Constant.COMMENT_TYPE_DEFAULT, Constant.COMMENT_PRAISE_ISDETAIL_YES);
        for (WorkComment workComment : workCommentList) {
            workCommentService.deleteComment(workComment.getId());
        }

        /*workNovel.setStatus(1);
        workNovelDao.save(workNovel);*/
        workNovelDao.delete(workNovel);

        return 1;
    }

    @Override
    public WorkNovel findOneById(Long id) {
        return workNovelDao.findOne(id);
    }
}
